﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using draw = System.Drawing;

/// <summary>
/// Summary description for MyGraphics
/// </summary>
public static class MyGraphics
{
  /// <summary>
  /// Ham nay sao chep 1 anh ra thanh 3 anh voi 3 kich co (big, normal,thumb)
  /// </summary>
  /// <param name="fileUpload"></param>
  /// <param name="strFileName"></param>
  public static string CopyFile(HttpPostedFile fileUpload,string folder, string strFileName)
  {
    string serverPath = MyConfig.serverPath;
    string fileName = Myfunction.toUrlFriendly(strFileName) + "-" + System.DateTime.Now.ToString().Replace("/", "").Replace(":", "").Replace(" ","");
    fileName += Path.GetExtension(fileUpload.FileName);
   
    //Copy Image big
    fileUpload.SaveAs(serverPath + folder + "/big/" + fileName);

    string sourceFile = "";
    if (!File.Exists(serverPath + folder + "/big/" + fileName))
    {
      return "null";
    }
    else
    {
      sourceFile = serverPath + folder + "/big/" + fileName;
    }

    draw.Image image = draw.Image.FromFile(sourceFile);   

    //Copy normal   

    draw.Image imageNormal = ResizeImage(image, MyConfig.ImageSizeNormal_Width, MyConfig.ImageSizeNormal_Height);
    imageNormal.Save(serverPath + folder + "/normal/" + fileName);
    imageNormal.Dispose();

    //Copy ImageThumb 
    draw.Image imageThumb = ResizeImage(image, MyConfig.ImageSizeThumb_Width, MyConfig.ImageSizeThumb_Height);
    imageThumb.Save(serverPath + folder + "/thumb/" + fileName);
    imageThumb.Dispose();

    image.Dispose();

    return fileName;
  }

  public static string CopyFileAvatar(HttpPostedFile fileUpload, string folder, string strFileName)
  {
    string serverPath = MyConfig.serverPath;
    string fileName = Myfunction.toUrlFriendly(strFileName) + "-" + System.DateTime.Now.ToString().Replace("/", "").Replace(":", "").Replace(" ", "");
    fileName += Path.GetExtension(fileUpload.FileName);

    fileUpload.SaveAs(serverPath + folder + "/big/" + fileName);

    string sourceFile = "";
    if (!File.Exists(serverPath + folder + "/big/" + fileName))
    {
      return "null";
    }
    else
    {
      sourceFile = serverPath + folder + "/big/" + fileName;
    }

    draw.Image image = draw.Image.FromFile(sourceFile);

    bool resizeWidth = (image.Width > image.Height ? true : false);
    int widthNew = (resizeWidth ? MyConfig.ImageSizeAvatar_Width : (MyConfig.ImageSizeAvatar_Height * image.Width / image.Height));
    int heightNew = (!resizeWidth ? 50 : (MyConfig.ImageSizeAvatar_Width * image.Height / image.Width));

    //Copy ImageThumb 
    draw.Image imageThumb = ResizeImage(image, widthNew, heightNew);
    imageThumb.Save(serverPath + folder + "/" + fileName);
    imageThumb.Dispose();

    image.Dispose();

    Myfunction.DeleteFile(folder + "/big",fileName);

    return fileName;
  }

  private static draw.Image ResizeImage(draw.Image image, int width, int height)
  {
    draw.Bitmap bmp = new draw.Bitmap(width, height);

    draw.Graphics graphic = draw.Graphics.FromImage((draw.Image)bmp);
    graphic.InterpolationMode = draw.Drawing2D.InterpolationMode.High;

    graphic.DrawImage(image, 0, 0, width, height);
    graphic.Dispose();

    return (draw.Image)bmp;
  }
}